#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <map>
#include <sstream>
#include <iomanip>

using namespace std;

class HighPrecisionDecimal {
private:
    string integer;
    string decimal;
    bool isNegative;

    void normalize() {
        // 标准化处理
        size_t firstNonZero = integer.find_first_not_of('0');
        integer = (firstNonZero == string::npos) ? "0" : integer.substr(firstNonZero);

        while (!decimal.empty() && decimal.back() == '0') {
            decimal.pop_back();
        }
    }

    // 字符串比较
    static int compare(const string& a, const string& b) {
        if (a.length() != b.length()) return a.length() < b.length() ? -1 : 1;
        return a.compare(b);
    }

    // 字符串加法
    static string add(string a, string b) {
        string result;
        int carry = 0;
        int i = static_cast<int>(a.size()) - 1;
        int j = static_cast<int>(b.size()) - 1;

        while (i >= 0 || j >= 0 || carry) {
            int sum = carry;
            if (i >= 0) sum += a[i--] - '0';
            if (j >= 0) sum += b[j--] - '0';
            carry = sum / 10;
            result.push_back(sum % 10 + '0');
        }
        reverse(result.begin(), result.end());
        return result;
    }

    // 字符串减法
    static string subtract(string a, string b) {
        bool negative = false;
        if (compare(a, b) < 0) {
            swap(a, b);
            negative = true;
        }
        
        string result;
        int borrow = 0;
        int i = static_cast<int>(a.size()) - 1;
        int j = static_cast<int>(b.size()) - 1;

        while (i >= 0) {
            int digit = a[i--] - '0' - borrow;
            borrow = 0;
            if (j >= 0) digit -= b[j--] - '0';
            if (digit < 0) {
                digit += 10;
                borrow = 1;
            }
            result.push_back(digit + '0');
        }
        
        reverse(result.begin(), result.end());
        
        // 移除前导零
        size_t firstNonZero = result.find_first_not_of('0');
        result = (firstNonZero == string::npos) ? "0" : result.substr(firstNonZero);
        
        if (negative && result != "0") {
            result = "-" + result;
        }
        
        return result;
    }

    // 字符串乘法
    static string multiply(const string& a, const string& b) {
        vector<int> res(a.size() + b.size(), 0);
        
        for (int i = a.size() - 1; i >= 0; i--) {
            for (int j = b.size() - 1; j >= 0; j--) {
                int mul = (a[i] - '0') * (b[j] - '0');
                int p1 = i + j, p2 = i + j + 1;
                int sum = mul + res[p2];
                
                res[p1] += sum / 10;
                res[p2] = sum % 10;
            }
        }
        
        string result;
        for (int digit : res) {
            if (!(result.empty() && digit == 0)) {
                result.push_back(digit + '0');
            }
        }
        
        return result.empty() ? "0" : result;
    }

public:
    // 一元负运算符
    HighPrecisionDecimal operator-() const {
        HighPrecisionDecimal result(*this);
        if (result.integer != "0" || !result.decimal.empty()) {
            result.isNegative = !result.isNegative;
        }
        return result;
    }

    HighPrecisionDecimal(const string& numStr = "0") {
        size_t start = 0;
        isNegative = false;

        if (!numStr.empty() && numStr[0] == '-') {
            isNegative = true;
            start = 1;
        }

        size_t dotPos = numStr.find('.', start);
        integer = (dotPos == string::npos) ? numStr.substr(start)
            : numStr.substr(start, dotPos - start);
        decimal = (dotPos == string::npos) ? "" : numStr.substr(dotPos + 1);

        if (integer.empty()) integer = "0";
        normalize();
    }

    // 加法运算符
    HighPrecisionDecimal operator+(const HighPrecisionDecimal& other) const {
        // 处理符号
        if (isNegative && !other.isNegative) {
            return other - (-(*this));
        }
        if (!isNegative && other.isNegative) {
            return *this - (-other);
        }

        HighPrecisionDecimal result;
        result.isNegative = isNegative; // 同号相加，结果符号不变

        // 对齐小数位
        size_t maxDecimalLen = max(decimal.length(), other.decimal.length());
        string thisDecimal = decimal + string(maxDecimalLen - decimal.length(), '0');
        string otherDecimal = other.decimal + string(maxDecimalLen - other.decimal.length(), '0');

        // 小数部分相加
        string sumDecimal = add(thisDecimal, otherDecimal);
        int carry = 0;
        
        // 处理小数部分进位
        if (sumDecimal.length() > maxDecimalLen) {
            carry = 1;
            sumDecimal = sumDecimal.substr(1);
        }

        // 整数部分相加
        string sumInteger = add(integer, other.integer);
        if (carry) {
            sumInteger = add(sumInteger, "1");
        }

        result.integer = sumInteger;
        result.decimal = sumDecimal;
        result.normalize();
        
        return result;
    }

    // 减法运算符
    HighPrecisionDecimal operator-(const HighPrecisionDecimal& other) const {
        // 处理符号
        if (isNegative && !other.isNegative) {
            HighPrecisionDecimal result = *this + (-other);
            result.isNegative = true;
            return result;
        }
        if (!isNegative && other.isNegative) {
            return *this + (-other);
        }
        if (isNegative && other.isNegative) {
            return (-other) - (-(*this));
        }

        // 比较绝对值
        string a = integer + decimal;
        string b = other.integer + other.decimal;
        
        // 对齐小数位数
        size_t maxDecimalLen = max(decimal.length(), other.decimal.length());
        a += string(maxDecimalLen - decimal.length(), '0');
        b += string(maxDecimalLen - other.decimal.length(), '0');
        
        bool resultNegative = false;
        if (compare(a, b) < 0) {
            swap(a, b);
            resultNegative = true;
        }
        
        // 执行减法
        string diffStr = subtract(a, b);
        
        // 格式化结果
        HighPrecisionDecimal result;
        if (diffStr.length() <= maxDecimalLen) {
            result.integer = "0";
            result.decimal = string(maxDecimalLen - diffStr.length(), '0') + diffStr;
        } else {
            result.integer = diffStr.substr(0, diffStr.length() - maxDecimalLen);
            result.decimal = diffStr.substr(diffStr.length() - maxDecimalLen);
        }
        
        result.isNegative = resultNegative;
        result.normalize();
        return result;
    }

    // 乘法运算符
    HighPrecisionDecimal operator*(const HighPrecisionDecimal& other) const {
        HighPrecisionDecimal result;
        
        // 处理符号
        result.isNegative = isNegative ^ other.isNegative;
        
        // 对齐小数位
        string a = integer + decimal;
        string b = other.integer + other.decimal;
        size_t decimalPos = decimal.length() + other.decimal.length();
        
        // 执行乘法
        string prod = multiply(a, b);
        
        // 处理小数点位置
        if (prod.length() <= decimalPos) {
            result.integer = "0";
            result.decimal = string(decimalPos - prod.length(), '0') + prod;
        } else {
            result.integer = prod.substr(0, prod.length() - decimalPos);
            result.decimal = prod.substr(prod.length() - decimalPos);
        }
        
        result.normalize();
        return result;
    }

    // 除法运算符
    HighPrecisionDecimal operator/(const HighPrecisionDecimal& other) const {
        if (other.integer == "0" && other.decimal.empty()) {
            throw runtime_error("Division by zero");
        }

        // 处理符号
        bool resultNegative = isNegative ^ other.isNegative;
        
        // 创建正数版本进行计算
        HighPrecisionDecimal dividend = *this;
        HighPrecisionDecimal divisor = other;
        dividend.isNegative = false;
        divisor.isNegative = false;

        // 将被除数和除数转换为整数
        string dividendStr = dividend.integer + dividend.decimal;
        string divisorStr = divisor.integer + divisor.decimal;
        
        // 确保被除数比除数长
        size_t shiftLen = divisor.decimal.length();
        dividendStr += string(shiftLen, '0');
        
        // 执行除法
        string quotient;
        string remainder = "0";
        map<string, size_t> remainders; // 用于检测循环小数
        size_t precision = 100; // 最大精度
        
        // 整数部分
        for (size_t i = 0; i < dividendStr.length(); i++) {
            remainder += dividendStr[i];
            
            // 移除前导零
            size_t firstNonZero = remainder.find_first_not_of('0');
            remainder = (firstNonZero == string::npos) ? "0" : remainder.substr(firstNonZero);
            
            // 计算当前位
            int digit = 0;
            while (compare(remainder, divisorStr) >= 0) {
                remainder = subtract(remainder, divisorStr);
                digit++;
            }
            
            quotient += to_string(digit);
        }
        
        // 如果是整除
        if (remainder == "0") {
            string result = quotient;
            HighPrecisionDecimal finalResult(result);
            finalResult.isNegative = resultNegative;
            return finalResult;
        }
        
        // 计算小数部分
        quotient += ".";
        size_t decimalPos = quotient.length();
        
        // 检测循环小数
        while (remainder != "0" && quotient.length() - decimalPos < precision) {
            // 记录当前余数位置
            if (remainders.count(remainder)) {
                size_t pos = remainders[remainder];
                quotient.insert(pos, "[");
                quotient += "]";
                break;
            }
            remainders[remainder] = quotient.length();
            
            remainder += "0";
            
            // 移除前导零
            size_t firstNonZero = remainder.find_first_not_of('0');
            remainder = (firstNonZero == string::npos) ? "0" : remainder.substr(firstNonZero);
            
            // 计算当前位
            int digit = 0;
            while (compare(remainder, divisorStr) >= 0) {
                remainder = subtract(remainder, divisorStr);
                digit++;
            }
            
            quotient += to_string(digit);
        }
        
        HighPrecisionDecimal finalResult(quotient);
        finalResult.isNegative = resultNegative;
        return finalResult;
    }

    string toString() const {
        string res;
        if (isNegative && (integer != "0" || !decimal.empty())) res += "-";
        res += integer;
        if (!decimal.empty()) res += "." + decimal;
        return res;
    }
};

class Calculator {
private:
    string expr;
    size_t pos = 0;

    char peekChar() {
        while (pos < expr.size() && isspace(expr[pos])) pos++;
        return pos < expr.size() ? expr[pos] : '\0';
    }

    void consumeChar() { pos++; }

    HighPrecisionDecimal parseExpression() {
        HighPrecisionDecimal left = parseTerm();
        while (true) {
            char op = peekChar();
            if (op == '+' || op == '-') {
                consumeChar();
                HighPrecisionDecimal right = parseTerm();
                left = (op == '+') ? left + right : left - right;
            }
            else break;
        }
        return left;
    }

    HighPrecisionDecimal parseTerm() {
        HighPrecisionDecimal left = parseFactor();
        while (true) {
            char op = peekChar();
            if (op == '*' || op == '/') {
                consumeChar();
                HighPrecisionDecimal right = parseFactor();
                left = (op == '*') ? left * right : left / right;
            }
            else break;
        }
        return left;
    }

    HighPrecisionDecimal parseFactor() {
        if (peekChar() == '(') {
            consumeChar();
            HighPrecisionDecimal result = parseExpression();
            if (peekChar() != ')') throw runtime_error("Mismatched parentheses");
            consumeChar();
            return result;
        }

        bool negative = false;
        if (peekChar() == '-') {
            negative = true;
            consumeChar();
        }

        string numStr;
        while (isdigit(peekChar()) || peekChar() == '.') {
            numStr += peekChar();
            consumeChar();
        }

        HighPrecisionDecimal num(numStr);
        return negative ? -num : num;
    }

public:
    HighPrecisionDecimal calculate(const string& input) {
        expr = input;
        pos = 0;
        HighPrecisionDecimal result = parseExpression();
        if (peekChar() != '\0') throw runtime_error("Invalid input");
        return result;
    }
};

int main() {
    Calculator calc;
    string input;

    cout << "高精度计算器 (支持循环小数和高精度运算)\n";
    cout << "示例: 3.1415926 * 3.1415926\n";
    cout << "      1/3 (将显示循环小数)\n";
    cout << "输入 'exit' 退出\n\n";

    while (true) {
        cout << "Calculator> ";
        if (!getline(cin, input) || input == "exit") break;
        if (input.empty()) continue;

        try {
            HighPrecisionDecimal result = calc.calculate(input);
            cout << result.toString() << endl;
        }
        catch (const exception& e) {
            cerr << "Error: " << e.what() << endl;
        }
    }
    return 0;
} 